package sample.ui

import android.app.Dialog
import android.content.Context
import android.content.Intent
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.WindowManager
import android.widget.TextView
import androidx.core.view.isVisible
import androidx.core.view.updateLayoutParams
import com.netease.core.R
import kotlinx.coroutines.script.tes.getColor
import kotlinx.coroutines.script.tes.phoneScreenWidth
import kotlinx.coroutines.script.tes.removeLastZero
import kotlinx.coroutines.script.tes.startAnimationScaleKick
import kotlinx.coroutines.script.tes.toast
import kotlin.math.max
import kotlin.math.min

class SetupSettingsDialog private constructor(context: Context, theme: Int) : Dialog(context, theme) {
    class Builder(private val context: Context) {
        private val canCancel = true

        fun create(): SetupSettingsDialog {
            val dialog = SetupSettingsDialog(context, R.style.Theme_Light_NoTitle_NoShadow_Dialog)
            val window = dialog.window
            if (window != null) {
                window.setWindowAnimations(R.style.Animation_Left_Rising)
                window.decorView.setPadding(0, 0, 0, 0)
                val lp = window.attributes
                lp.width = WindowManager.LayoutParams.WRAP_CONTENT
                lp.height = WindowManager.LayoutParams.MATCH_PARENT
                window.attributes = lp
                window.setGravity(Gravity.START)
            }
            val view = LayoutInflater.from(context).inflate(R.layout.ssd, null)
            view.findViewById<View>(R.id.scrollView).updateLayoutParams { width = (phoneScreenWidth * 0.64).toInt() }
            view.findViewById<GABPanelV2Layout>(R.id.gabPanelLayout).setup()
            updateUI(view)
            dialog.setContentView(view)
            dialog.setCanceledOnTouchOutside(canCancel)
            dialog.setCancelable(canCancel)
            return dialog
        }



        private fun updateUI(view: View) {
            view.findViewById<View>(R.id.increaseSceneClearUp).setOnClickListener {
                it.startAnimationScaleKick()
                Properties.setSceneClearUpInterval(Properties.sceneClearUpInterval + 1)
                updateUI(view)
            }
            view.findViewById<View>(R.id.decreaseSceneClearUp).setOnClickListener {
                it.startAnimationScaleKick()
                Properties.setSceneClearUpInterval(max(0, Properties.sceneClearUpInterval - 1))
                updateUI(view)
            }
            view.findViewById<View>(R.id.increaseSceneContinueWait).setOnClickListener {
                it.startAnimationScaleKick()
                Properties.setSceneContinueWait(max(0, Properties.sceneContinueWait + 1))
                updateUI(view)
            }
            view.findViewById<View>(R.id.decreaseSceneContinueWait).setOnClickListener {
                it.startAnimationScaleKick()
                Properties.setSceneContinueWait(max(0, Properties.sceneContinueWait - 1))
                updateUI(view)
            }
//            view.findViewById<View>(R.id.increaseImmKeyPos).setOnClickListener {
//                it.startAnimationScaleKick()
//                Properties.setImmKeyPos(min(18, Properties.immKeyPos + 1))
//                updateUI(view)
//            }
//            view.findViewById<View>(R.id.decreaseImmKeyPos).setOnClickListener {
//                it.startAnimationScaleKick()
//                Properties.setImmKeyPos(max(1, Properties.immKeyPos - 1))
//                updateUI(view)
//            }
//            view.findViewById<View>(R.id.skipRandomYes).setOnClickListener {
//                it.startAnimationScaleKick()
//                Properties.setSkipRandomMode(true)
//                updateUI(view)
//            }
//            view.findViewById<View>(R.id.skipRandomNo).setOnClickListener {
//                it.startAnimationScaleKick()
//                Properties.setSkipRandomMode(false)
//                updateUI(view)
//            }
//            view.findViewById<View>(R.id.increaseCastleClearUp).setOnClickListener {
//                it.startAnimationScaleKick()
//                Properties.setCastleClearUpInterval(Properties.castleClearUpInterval + 1)
//                updateUI(view)
//            }
//            view.findViewById<View>(R.id.decreaseCastleClearUp).setOnClickListener {
//                it.startAnimationScaleKick()
//                Properties.setCastleClearUpInterval(max(0, Properties.castleClearUpInterval - 1))
//                updateUI(view)
//            }
//            view.findViewById<View>(R.id.increaseCastleStopTime).setOnClickListener {
//                it.startAnimationScaleKick()
//                Properties.setCastleStopTime(Properties.castleStopTime + 10)
//                updateUI(view)
//            }
//            view.findViewById<View>(R.id.decreaseCastleStopTime).setOnClickListener {
//                it.startAnimationScaleKick()
//                Properties.setCastleStopTime(max(0, Properties.castleStopTime - 10))
//                updateUI(view)
//            }
//
//            view.findViewById<View>(R.id.fishingLv1).setOnClickListener {
//                it.startAnimationScaleKick()
//                Properties.setFishingLv(1)
//                updateUI(view)
//            }
//            view.findViewById<View>(R.id.fishingLv2).setOnClickListener {
//                it.startAnimationScaleKick()
//                Properties.setFishingLv(2)
//                updateUI(view)
//            }
//            view.findViewById<View>(R.id.fishingLv3).setOnClickListener {
//                it.startAnimationScaleKick()
//                Properties.setFishingLv(3)
//                updateUI(view)
//            }
//            view.findViewById<View>(R.id.fishingLv4).setOnClickListener {
//                it.startAnimationScaleKick()
//                Properties.setFishingLv(4)
//                updateUI(view)
//            }
//
//            view.findViewById<View>(R.id.decreaseRs).setOnClickListener {
//                it.startAnimationScaleKick()
//                Properties.setRoleSpeed(max(50, Properties.roleSpeed - 1))
//                updateUI(view)
//                toast("若你不懂此选项的效果本质，请恢复到默认值。因为你大概率在做错误的事情。")
//            }
//            view.findViewById<View>(R.id.increaseRs).setOnClickListener {
//                it.startAnimationScaleKick()
//                Properties.setRoleSpeed(min(200, Properties.roleSpeed + 1))
//                updateUI(view)
//                toast("若你不懂此选项的效果本质，请恢复到默认值。因为你大概率在做错误的事情。")
//            }
            view.findViewById<View>(R.id.timeout10m).setOnClickListener {
                it.startAnimationScaleKick()
                Properties.setScriptStopTimeout(600)
                updateUI(view)
            }
            view.findViewById<View>(R.id.timeout15m).setOnClickListener {
                it.startAnimationScaleKick()
                Properties.setScriptStopTimeout(900)
                updateUI(view)
            }
            view.findViewById<View>(R.id.timeout30m).setOnClickListener {
                it.startAnimationScaleKick()
                Properties.setScriptStopTimeout(1800)
                updateUI(view)
            }
            view.findViewById<View>(R.id.timeout1h).setOnClickListener {
                it.startAnimationScaleKick()
                Properties.setScriptStopTimeout(3600)
                updateUI(view)
            }
            view.findViewById<View>(R.id.timeout3h).setOnClickListener {
                it.startAnimationScaleKick()
                Properties.setScriptStopTimeout(3*3600)
                updateUI(view)
            }
            view.findViewById<View>(R.id.timeout24h).setOnClickListener {
                it.startAnimationScaleKick()
                Properties.setScriptStopTimeout(24*3600)
                updateUI(view)
            }
            view.findViewById<View>(R.id.retryNo).setOnClickListener {
                it.startAnimationScaleKick()
                Properties.setNetRetryCount(max(0, Properties.netRetryCount - 1))
                updateUI(view)
            }
            view.findViewById<View>(R.id.retryYes).setOnClickListener {
                it.startAnimationScaleKick()
                Properties.setNetRetryCount(Properties.netRetryCount + 1)
                updateUI(view)
            }

            view.findViewById<View>(R.id.reLaunchYes).setOnClickListener {
                it.startAnimationScaleKick()
                Properties.setTryLaunchAfterUnknown(true)
                updateUI(view)
            }
            view.findViewById<View>(R.id.reLaunchNo).setOnClickListener {
                it.startAnimationScaleKick()
                Properties.setTryLaunchAfterUnknown(false)
                updateUI(view)
            }
            view.findViewById<View>(R.id.increaseStop).setOnClickListener {
                it.startAnimationScaleKick()
                Properties.setStopCountdown(min(Properties.stopCountdown + 1000, 100000))
                updateUI(view)
            }
            view.findViewById<View>(R.id.decreaseStop).setOnClickListener {
                it.startAnimationScaleKick()
                Properties.setStopCountdown(max(0, Properties.stopCountdown - 1000))
                updateUI(view)
            }
//            view.findViewById<View>(R.id.increaseRefreshFrameTs).setOnClickListener {
//                it.startAnimationScaleKick()
//                Properties.setRefreshFrameInterval(min(2000, Properties.refreshFrameInterval + 20))
//                updateUI(view)
//            }
//            view.findViewById<View>(R.id.decreaseRefreshFrameTs).setOnClickListener {
//                it.startAnimationScaleKick()
//                Properties.setRefreshFrameInterval(max(400, Properties.refreshFrameInterval - 20))
//                updateUI(view)
//            }
            view.findViewById<View>(R.id.decreaseSceneMode).setOnClickListener {
                it.startAnimationScaleKick()
                Properties.setForceSceneMode(max(1, Properties.forceSceneMode - 1))
                updateUI(view)
            }
            view.findViewById<View>(R.id.increaseSceneMode).setOnClickListener {
                it.startAnimationScaleKick()
                Properties.setForceSceneMode(min(2, Properties.forceSceneMode + 1))
                updateUI(view)
            }
//            view.findViewById<View>(R.id.decreaseLxEnterPos).setOnClickListener {
//                it.startAnimationScaleKick()
//                Properties.setLxEnterPos(max(0, Properties.lxEnterPos - 1))
//                updateUI(view)
//            }
//            view.findViewById<View>(R.id.increaseLxEnterPos).setOnClickListener {
//                it.startAnimationScaleKick()
//                Properties.setLxEnterPos(min(1, Properties.lxEnterPos + 1))
//                updateUI(view)
//            }
            view.findViewById<View>(R.id.scenePetClearYes).setOnClickListener {
                it.startAnimationScaleKick()
                Properties.setScenePetClear(true)
                updateUI(view)
            }
            view.findViewById<View>(R.id.scenePetClearNo).setOnClickListener {
                it.startAnimationScaleKick()
                Properties.setScenePetClear(false)
                updateUI(view)
            }
//            view.findViewById<View>(R.id.sceneSkill800Yes).setOnClickListener {
//                it.startAnimationScaleKick()
//                Properties.setSceneSkill800(true)
//                updateUI(view)
//            }
//            view.findViewById<View>(R.id.sceneSkill800No).setOnClickListener {
//                it.startAnimationScaleKick()
//                Properties.setSceneSkill800(false)
//                updateUI(view)
//            }
//            view.findViewById<View>(R.id.decreaseScriptSleepInterval).setOnClickListener {
//                it.startAnimationScaleKick()
//                Properties.setScriptSleepInterval(max(0, Properties.scriptSleepInterval - 1))
//                updateUI(view)
//            }
//            view.findViewById<View>(R.id.increaseScriptSleepInterval).setOnClickListener {
//                it.startAnimationScaleKick()
//                Properties.setScriptSleepInterval(Properties.scriptSleepInterval + 1)
//                updateUI(view)
//            }
//            view.findViewById<View>(R.id.decreaseScriptSleepUnitTs).setOnClickListener {
//                it.startAnimationScaleKick()
//                Properties.setScriptSleepUnitTs(max(0f, Properties.scriptSleepUnitTs - 0.5f))
//                updateUI(view)
//            }
//            view.findViewById<View>(R.id.increaseScriptSleepUnitTs).setOnClickListener {
//                it.startAnimationScaleKick()
//                Properties.setScriptSleepUnitTs(Properties.scriptSleepUnitTs + 0.5f)
//                updateUI(view)
//            }
//            view.findViewById<View>(R.id.increaseScriptSleepAtkInterval).setOnClickListener {
//                it.startAnimationScaleKick()
//                Properties.setScriptSleepAtkInterval(min(3f, Properties.scriptSleepAtkInterval + 0.1f))
//                updateUI(view)
//            }
//            view.findViewById<View>(R.id.decreaseScriptSleepAtkInterval).setOnClickListener {
//                it.startAnimationScaleKick()
//                Properties.setScriptSleepAtkInterval(max(0.3f, Properties.scriptSleepAtkInterval - 0.1f))
//                updateUI(view)
//            }
//            view.findViewById<View>(R.id.playerQCZZYes).setOnClickListener {
//                it.startAnimationScaleKick()
//                Properties.setPlayerQCZZ(true)
//                updateUI(view)
//            }
//            view.findViewById<View>(R.id.playerQCZZNo).setOnClickListener {
//                it.startAnimationScaleKick()
//                Properties.setPlayerQCZZ(false)
//                updateUI(view)
//            }
            view.findViewById<View>(R.id.npcAssistYes).setOnClickListener {
                it.startAnimationScaleKick()
                Properties.setUseNpcAssist(true)
                updateUI(view)
            }
            view.findViewById<View>(R.id.npcAssistNo).setOnClickListener {
                it.startAnimationScaleKick()
                Properties.setUseNpcAssist(false)
                updateUI(view)
            }

//            view.findViewById<View>(R.id.dyContinue0).setOnClickListener {
//                it.startAnimationScaleKick()
//                Properties.setAutoDYContinueTime(0L)
//                updateUI(view)
//            }
//            view.findViewById<View>(R.id.dyContinue29).setOnClickListener {
//                it.startAnimationScaleKick()
//                Properties.setAutoDYContinueTime(1740000L)
//                updateUI(view)
//            }
//            view.findViewById<View>(R.id.dyContinue59).setOnClickListener {
//                it.startAnimationScaleKick()
//                Properties.setAutoDYContinueTime(3540000L)
//                updateUI(view)
//            }
//            view.findViewById<View>(R.id.dyContinue89).setOnClickListener {
//                it.startAnimationScaleKick()
//                Properties.setAutoDYContinueTime(5340000L)
//                updateUI(view)
//            }
//
//            view.findViewById<View>(R.id.increaseDyZZEdge).setOnClickListener {
//                it.startAnimationScaleKick()
//                Properties.setDYZZEdge(min(20, Properties.dyZZEdge + 1))
//                updateUI(view)
//            }
//            view.findViewById<View>(R.id.decreaseDyZZEdge).setOnClickListener {
//                it.startAnimationScaleKick()
//                Properties.setDYZZEdge(max(0, Properties.dyZZEdge - 1))
//                updateUI(view)
//            }
//            view.findViewById<View>(R.id.decreaseImmRestart).setOnClickListener {
//                it.startAnimationScaleKick()
//                Properties.setImmRestart(max(0, Properties.immRestart - 1))
//                updateUI(view)
//            }
//            view.findViewById<View>(R.id.increaseImmRestart).setOnClickListener {
//                it.startAnimationScaleKick()
//                Properties.setImmRestart(min(1, Properties.immRestart + 1))
//                updateUI(view)
//            }

//            view.findViewById<TextView>(R.id.immKeyPosValue).text = Properties.immKeyPos.toString()
            view.findViewById<TextView>(R.id.sSceneClearUpIntervalValue).text = if (Properties.sceneClearUpInterval <= 0) "不分解" else Properties.sceneClearUpInterval.toString()
            view.findViewById<TextView>(R.id.sSceneContinueWait).text = if (Properties.sceneContinueWait <= 0) "不等待" else Properties.sceneContinueWait.toString()
//            view.findViewById<TextView>(R.id.skipRandomYes).setTextColor(getColor(if (Properties.skipRandomMode) "#ffd700" else "#44cdcdcd"))
//            view.findViewById<TextView>(R.id.skipRandomNo).setTextColor(getColor(if (!Properties.skipRandomMode) "#ffd700" else "#44cdcdcd"))
//            view.findViewById<TextView>(R.id.immKeyPosLayout).isVisible =  true//isImm || isTZ
            view.findViewById<View>(R.id.sceneLayout).isVisible = true// !(isImm || isTZ) && !isSceneMY && !isCastleDef && !isFishing
//            view.findViewById<View>(R.id.castleLayout).isVisible = true// isCastleDef
//            view.findViewById<TextView>(R.id.castleClearUpIntervalValue).text = if (Properties.castleClearUpInterval <= 0) "不分解" else Properties.castleClearUpInterval.toString()
//            view.findViewById<TextView>(R.id.castleStopTimeValue).text = if (Properties.castleStopTime == 0) "不启用" else Properties.castleStopTime.toString()

            view.findViewById<TextView>(R.id.retryValue).text = when {
                Properties.netRetryCount <= 0 -> "不重连"
                else -> Properties.netRetryCount.toString()
            }
            view.findViewById<TextView>(R.id.stopValue).text = if (Properties.stopCountdown <= 0) "不停止" else "${Properties.stopCountdown}"
//            view.findViewById<TextView>(R.id.roleSpeedValue).text = "${Properties.roleSpeed}%"
            view.findViewById<TextView>(R.id.reLaunchYes).setTextColor(getColor(if (Properties.tryLaunchAfterUnknown) "#ffd700" else "#44cdcdcd"))
            view.findViewById<TextView>(R.id.reLaunchNo).setTextColor(getColor(if (!Properties.tryLaunchAfterUnknown) "#ffd700" else "#44cdcdcd"))
//            view.findViewById<TextView>(R.id.refreshFrameTsValue).text = Properties.refreshFrameInterval.toString()
            view.findViewById<TextView>(R.id.timeout10m).setTextColor(getColor(if (Properties.scriptStopTimeout == 600) "#ffd700" else "#44cdcdcd"))
            view.findViewById<TextView>(R.id.timeout15m).setTextColor(getColor(if (Properties.scriptStopTimeout == 900) "#ffd700" else "#44cdcdcd"))
            view.findViewById<TextView>(R.id.timeout30m).setTextColor(getColor(if (Properties.scriptStopTimeout == 1800) "#ffd700" else "#44cdcdcd"))
            view.findViewById<TextView>(R.id.timeout1h).setTextColor(getColor(if (Properties.scriptStopTimeout == 3600) "#ffd700" else "#44cdcdcd"))
            view.findViewById<TextView>(R.id.timeout3h).setTextColor(getColor(if (Properties.scriptStopTimeout == 3*3600) "#ffd700" else "#44cdcdcd"))
            view.findViewById<TextView>(R.id.timeout24h).setTextColor(getColor(if (Properties.scriptStopTimeout == 24*3600) "#ffd700" else "#44cdcdcd"))

            view.findViewById<TextView>(R.id.sceneModeValue).text = if (Properties.forceSceneMode == 1) "普通" else if (Properties.forceSceneMode == 2) "远古" else "忽略"
//            view.findViewById<TextView>(R.id.lxEnterPosValue).text = if (Properties.lxEnterPos == 0) "未领取活动奖励" else "已领取"
            view.findViewById<TextView>(R.id.scenePetClearYes).setTextColor(getColor(if (Properties.scenePetClear) "#ffd700" else "#44cdcdcd"))
            view.findViewById<TextView>(R.id.scenePetClearNo).setTextColor(getColor(if (!Properties.scenePetClear) "#ffd700" else "#44cdcdcd"))
//            view.findViewById<TextView>(R.id.sceneSkill800Yes).setTextColor(getColor(if (Properties.sceneSkill800) "#ffd700" else "#44cdcdcd"))
//            view.findViewById<TextView>(R.id.sceneSkill800No).setTextColor(getColor(if (!Properties.sceneSkill800) "#ffd700" else "#44cdcdcd"))
//            view.findViewById<TextView>(R.id.scriptSleepIntervalValue).text = if (Properties.scriptSleepInterval <= 0) "不停顿" else Properties.scriptSleepInterval.toString()
//            view.findViewById<TextView>(R.id.scriptSleepUnitTsValue).text = if (Properties.scriptSleepUnitTs <= 0) "不等待" else Properties.scriptSleepUnitTs.toString().removeLastZero
//            view.findViewById<TextView>(R.id.scriptSleepAtkIntervalValue).text = Properties.scriptSleepAtkInterval.toString()
//            view.findViewById<TextView>(R.id.playerQCZZYes).setTextColor(getColor(if (Properties.playerQCZZ) "#ffd700" else "#44cdcdcd"))
//            view.findViewById<TextView>(R.id.playerQCZZNo).setTextColor(getColor(if (!Properties.playerQCZZ) "#ffd700" else "#44cdcdcd"))
            view.findViewById<TextView>(R.id.npcAssistYes).setTextColor(getColor(if (Properties.useNpcAssist) "#ffd700" else "#44cdcdcd"))
            view.findViewById<TextView>(R.id.npcAssistNo).setTextColor(getColor(if (!Properties.useNpcAssist) "#ffd700" else "#44cdcdcd"))
//            view.findViewById<TextView>(R.id.dyContinue0).setTextColor(getColor(if (Properties.autoDYContinueTime == 0L) "#ffd700" else "#44cdcdcd"))
//            view.findViewById<TextView>(R.id.dyContinue29).setTextColor(getColor(if (Properties.autoDYContinueTime == 1740000L) "#ffd700" else "#44cdcdcd"))
//            view.findViewById<TextView>(R.id.dyContinue59).setTextColor(getColor(if (Properties.autoDYContinueTime == 3540000L) "#ffd700" else "#44cdcdcd"))
//            view.findViewById<TextView>(R.id.dyContinue89).setTextColor(getColor(if (Properties.autoDYContinueTime == 5340000L) "#ffd700" else "#44cdcdcd"))
//            view.findViewById<TextView>(R.id.dyZZEdgeValue).text = if (Properties.dyZZEdge <= 0) "不移动" else Properties.dyZZEdge.toString()
//            view.findViewById<TextView>(R.id.immRestartValue).text = if (Properties.immRestart == 0) "返回秘境入口" else "限时重刷秘境"
//            view.findViewById<TextView>(R.id.fishingLv1).setTextColor(getColor(if (Properties.fishingLv == 1) "#ffd700" else "#44cdcdcd"))
//            view.findViewById<TextView>(R.id.fishingLv2).setTextColor(getColor(if (Properties.fishingLv == 2) "#ffd700" else "#44cdcdcd"))
//            view.findViewById<TextView>(R.id.fishingLv3).setTextColor(getColor(if (Properties.fishingLv == 3) "#ffd700" else "#44cdcdcd"))
//            view.findViewById<TextView>(R.id.fishingLv4).setTextColor(getColor(if (Properties.fishingLv == 4) "#ffd700" else "#44cdcdcd"))
        }
    }
}